home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / MAXMEM__ / MAXAPPLZ.C1 < prev   
Text File  |  1988-11-07  |  11KB  |  275 lines

  1. /* MaxApplZone.c                                                        */
  2. /* ==================================================================== */    
  3. /*  basic HyperCard memory management function                          */
  4. /*  ⌐ Copyright 1988 Sam Thornton, PO Box 123, Fairfield, NE 68938-0123 */
  5. /*  May be copied & distributed without charge only. Not for commercial */
  6. /*  sale or resale.                                                     */
  7. /*  Written in LSC 3.01 11/2/88 - some portions ⌐ by Symantec           */
  8. /* ==================================================================== */
  9. /*    SYNTAX:  get MaxApplZone()                                          */
  10. /*             Puts up a modeless dialog that displays current memory   */
  11. /*             available and current time.  Clicking on box next to     */
  12. /*             MEMORY compacts the application heap.  Clicking on the   */
  13. /*             box next to the TIME display momentarily displays the    */
  14. /*             current date (MM/DD/YY). Returns NULL.                   */
  15. /*                                                                      */
  16. /*           get MaxApplZone("C")                                       */
  17. /*             Bypasses the modeless dialog, compacts the application   */
  18. /*             heap and returns the number of KB free.                  */
  19. /*                                                                      */
  20. /*           get MaxApplZone ( < anything except 'C' > )                */
  21. /*             Bypasses modeless dialog and memory compaction to return */
  22. /*             the number of KB free.                                   */
  23. /*                                                                      */
  24. /*           If all you need are the latter two functions, see the      */
  25. /*           stripped down version in the accompanying file:            */
  26. /*           <MaxApplZone.simple.c>                                     */
  27. /*                                                                      */
  28. /*  NOTE that, due to some unavoidable heap fragmentation, the number   */
  29. /*  of KB free returned or displayed is normally a little bit more      */
  30. /*  than is actually usable.                                            */
  31. /*                                                                      */
  32. /*  XCMD glue is based on the file LSCXCM.SIT/binary, modified slightly */
  33. /*  to partially dereference stringpointers...makes for slightly less   */
  34. /*  distracting way to do some of the callback routines, e.g.:          */
  35. /*                                                                      */
  36. /*   the proto function:                                                */
  37. /*      pascal void SendCardMessage(paramPtr, msg)                      */
  38. /*      register XCmdBlockPtr paramPtr; Ptr msg; (vs. StringPtr msg;)   */
  39. /*                                      -----------------------------   */
  40. /*  ...allows: SendCardMessage(paramPtr, "\pgo next card");             */
  41. /*  without any other contortions.                                      */
  42. /*                                                                      */
  43. /*  In your LSC PROJECT file, make sure to add the following files:     */
  44. /*     <MacTraps>, <XCMD glue file/library>, <unixst2d.c>               */
  45. /*  and set the Project Type to: CODE/XFCN, ID nnn, NAME: MaxApplZone   */
  46. /* ==================================================================== */
  47.  
  48. #include "HyperXCmd.h"
  49. #include "SetUpA4.h"
  50. #include "MemoryMgr.h"
  51. #include "WindowMgr.h"
  52. #include "DialogMgr.h"
  53. #include "EventMgr.h"
  54. #include "MacTypes.h"
  55. #include "QuickDraw.h"
  56. #include "unix.h"
  57.  
  58. /* ===================================================================== */
  59. /* SOME CONSTANTS...
  60. /* ===================================================================== */
  61. #define    MAIN_DIALOG 297        /* RESOURCE ID FOR OUR DIALOG                */
  62. #define _DATE_TIME 2        /* CORRESPONDS TO BOX ITEMS IN OUR ITEM LIST */
  63. #define _COMPACT 1
  64. #define _QUIT 9                /* ARBITRARY VALUE FOR QUIT FLAG             */
  65. /* ===================================================================== */
  66.  
  67. /* ===================================================================== */
  68. /* SET ASIDE SOME GLOBALS FOR LAZIER PARAMETER MANAGEMENT                */
  69. /* ===================================================================== */
  70. static    int                dt_mode;        /* FLAG: DISPLAY DATE OR TIME    */
  71. static    long            the_ticks;        /* OUR GLOBAL COUNTDOWN TIMER    */
  72. static    Rect            myGlobalRect;    /* H/C SCREEN DISPLAY AREA       */
  73. static    XCmdBlockPtr    myParamPtr;        /* COPY OF HYPERCARD CALLBACK    */
  74. /*                                           POINTER FOR FILTERPROC        */
  75. /* ===================================================================== */
  76.  
  77.  
  78. /* ===================================================================== */
  79. /* ===================================================================== */
  80. /*                              ENTRYPOINT                               */
  81. /* ===================================================================== */
  82. /* ===================================================================== */
  83.  
  84. pascal void main (paramPtr)
  85. XCmdBlockPtr paramPtr;
  86.   {
  87.     int                    itemHit, mem_size;
  88.       pascal Boolean        MyFilter();
  89.       DialogPtr            myDialogPtr;
  90.       GrafPtr                current_port;
  91.  
  92.       RememberA0();
  93.     SetUpA4();
  94.     
  95. /* ===================================================================== */    
  96. /* IF ANY PARAMETER IS RECEIVED, SKIP DIALOG & RETURN MEMSIZE            */
  97. /* ===================================================================== */
  98.  
  99.     if (paramPtr->paramCount != 0) {
  100.     
  101.     /* ...BUT IF THE PARAMETER STARTS WITH 'C', WE WANT TO COMPACT FIRST */
  102.         if (**(paramPtr->params[0]) == 'C' || **(paramPtr->params[0]) == 'c')
  103.             compact_it();
  104.             
  105.     /* RETURN AVAILABLE MEMORY IN KB (plenty of room for expansions)     */
  106.         paramPtr->returnValue = NewHandle(10);
  107.         if (paramPtr->returnValue == 0L) /* ... just to make sure */
  108.             SysBeep(1);
  109.         else {
  110.             mem_size = (TheZone->zcbFree) / 1024;
  111.             stci_d(*(paramPtr->returnValue), mem_size, 9);
  112.             }
  113.         } /* FALL THRU TO EXIT...*/
  114.         
  115. /* ===================================================================== */
  116. /* ...OTHERWISE, PUT UP OUR DIALOG BOX AND RUN IT                        */
  117. /* ===================================================================== */
  118.  
  119.     else if (paramPtr->paramCount == 0) {
  120.     
  121.           myDialogPtr = GetNewDialog(MAIN_DIALOG, 0, -1L);
  122.           
  123.       /* CHECK TO MAKE SURE THERE WAS ENOUGH MEMORY */
  124.           if (myDialogPtr == 0L) { /* oops! -- better bail out */
  125.               SysBeep(10);
  126.               RestoreA4();
  127.               return;
  128.               }
  129.     
  130.     /* MAKE HYPERCARD CALLBACK ADDRESS AVAILABLE TO MODALDIALOG FILTER */
  131.         myParamPtr = paramPtr;
  132.     
  133.     /* SET DATE/TIME FLAG FOR DISPLAY OF CURRENT TIME: 0=time, 1=date */
  134.         dt_mode = 0;
  135.     
  136.     /* DESCRIBE THE AREA IN WHICH OUR DIALOG CAN BE MOVED */
  137.     /* NOTE: can't seem to use 'screenBits.bounds' from HyperCard, so... */
  138.         GetPort(¤t_port);
  139.         myGlobalRect = current_port->portRect;
  140.         myGlobalRect.top += 20;
  141.         InsetRect (&myGlobalRect, 4, 4);
  142.     
  143.         InitCursor (); /* SET CURSOR TO ARROW */
  144.             
  145.           show_mem(myDialogPtr);        /* setup dialog (see below) */
  146.           ShowWindow(myDialogPtr);    /* dialog is initially invisible */
  147.       
  148.       /* LOOP THRU MODALDIALOG UNTIL CLOSEBOX IS CLICKED */
  149.           do {
  150.  
  151.             ModalDialog(MyFilter, &itemHit);
  152.  
  153.         } while (itemHit != _QUIT);
  154.         
  155.         DisposDialog(myDialogPtr);
  156.     }
  157. /* ===================================================================== */
  158. /*                       EXIT BACK TO HYPERCARD                          */
  159. /* ===================================================================== */
  160.     RestoreA4();
  161.     return;
  162. }
  163.  
  164. /* ===================================================================== */
  165. /* ===================================================================== */
  166. /*                            SUBROUTINES                                */
  167. /* ===================================================================== */
  168. /* ===================================================================== */
  169.  
  170.  
  171. /* ===================================================================== */
  172. /* MODALDIALOG FILTERPROC HANDLES OUR MEAGER REQUIREMENTS                */
  173. /* ===================================================================== */
  174.  
  175. pascal static Boolean MyFilter(dptr, theEvent, itemHit)
  176. DialogPtr dptr; EventRecord *theEvent; int *itemHit;
  177. {
  178.     int theHit; /* LOCAL SPARE */
  179.         
  180.     if (Ticks >= the_ticks) show_mem(dptr); /* IF TIMER ELAPSED, UPDATE */
  181.     
  182.     /* IF NO MOUSEHIT, LET MODALDIALOG HANDLE THIS CYCLE */
  183.     if (theEvent->what != mouseDown) return (false);
  184.     
  185.     /* FIND OUT WHERE THE MOUSE WAS CLICKED AND HANDLE IT */
  186.     /* NOTE:  Since this is strictly a ModalDialog subroutine, we don't
  187.               have to worry about hits in other parts of the screen */
  188.               
  189.     switch(FindWindow(theEvent->where, &dptr)) {
  190.         case inDrag:    /* DRAG DIALOG AROUND - NOTE: callback routine
  191.                            modified in XCMD.c to dereference msg param */
  192.         
  193.             DragWindow(dptr, theEvent->where, &myGlobalRect);
  194.             /* lazy update of HC screen */
  195.             SendCardMessage(myParamPtr, "\pgo this card");
  196.             return(true); 
  197.             
  198.         case inGoAway: /* SET ITEMHIT TO OUR QUIT FLAG, IF APPROPRIATE */
  199.             if (TrackGoAway(dptr, theEvent->where)) *itemHit = _QUIT;
  200.             return(true);
  201.                 
  202.         case inContent: /* RESPOND TO HITS IN OUR DIALOG CONTENT REGION */
  203.             if (DialogSelect(theEvent, &dptr, &theHit)) {
  204.             
  205.                 switch(theHit) {
  206.         
  207.                 case _COMPACT:
  208.                     comp_Mem(dptr);
  209.                     return(true);
  210.                 
  211.                 case _DATE_TIME:
  212.                     dt_mode = 1;
  213.                     show_mem(dptr);
  214.                     return(true);
  215.                 
  216.                 } /* END OF 'IN CONTENT' SWITCH */
  217.         
  218.         } /* END OF 'IF DIALOGSELECT' */
  219.         
  220.     } /* END OF FINDWINDOW SWITCH */
  221.     
  222.     return(false); /* nothing interesting happened--let's bore ModalDialog */
  223.     
  224. } /* END OF FUNCTION */
  225.  
  226. /* ===================================================================== */
  227. /* COMPACT THE APPLICATION HEAP AND JSR TO DISPLAY ROUTINE               */
  228. /* ===================================================================== */
  229.  
  230. comp_Mem(whichWindow)
  231. DialogPtr whichWindow;
  232. {
  233.     compact_it();
  234.     show_mem(whichWindow);
  235.     return;
  236. }
  237.  
  238. /* ===================================================================== */
  239. /* COMPACT THE APPLICATION HEAP                                          */
  240. /* ===================================================================== */
  241.  
  242. compact_it()
  243. {
  244.     Size    grow;
  245.     
  246.     MaxMem(&grow);
  247.     MaxApplZone();
  248.     return;
  249. }
  250.  
  251. /* ===================================================================== */
  252. /* FUNCTION TO DISPLAY CURRENT MEMSIZE & TIME (OR DATE) IN OUR DIALOG    */
  253. /* ===================================================================== */
  254.  
  255. show_mem(whichWindow)
  256. DialogPtr whichWindow;
  257. {
  258.     char    mem_string[11];
  259.     char    time_string[11];
  260.  
  261.     NumToString(((TheZone->zcbFree) / 1024),&mem_string); /* AVAIL. MEM */
  262.     if (dt_mode == 0) /* WE EITHER WANT THE CURRENT TIME, OR...*/
  263.         IUTimeString(Time, 1, &time_string); /* '1' = HH:MM:SS format */
  264.     else if (dt_mode == 1) { /* ...WE WANT THE CURRENT DATE */
  265.         IUDateString(Time, 0, &time_string); /* '0' = MM/DD/YY format */
  266.         dt_mode = 0;
  267.         }
  268.         
  269. /* THIS IS PURTY KLUNKY - PROLLY NEED TO DRAWR IT DIREKLY */
  270.     ParamText(mem_string,time_string,"","");
  271.     DrawDialog(whichWindow);
  272.     the_ticks = Ticks + 55; /* RESET OUR TIMER */
  273.     return;
  274. }
  275. /* ===================================================================== */